package com.primeton.wh.mcenter.impl.message.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.primeton.wh.mcenter.api.model.Message;
import com.primeton.wh.mcenter.impl.message.mapper.MessageMapper;
import com.primeton.wh.mcenter.impl.message.util.MessageConstants;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2020-03-16 23:23
 * @description:
 **/
@Component
public class MessageDAO {

    @Resource
    private MessageMapper messageMapper;

    public IPage<Message> getMessageList(Map<String, Object> condition) throws Throwable {
        Long currentPage = (Long) condition.get("page");
        Long currentPageSize = (Long) condition.get("limit");

        String msgTitle = condition.get("msgTitle") != null ? condition.get("msgTitle").toString() : "";
        String startDate = condition.get("startDate") != null ? condition.get("startDate").toString() : "";
        String endDate = condition.get("endDate") != null ? condition.get("endDate").toString() : "";
        String appCode = condition.get("appCode") != null ? condition.get("appCode").toString() : "";
        String msgTo = condition.get("msgTo") != null ? condition.get("msgTo").toString() : "";


        IPage<Message> page = new Page(currentPage, currentPageSize);

        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(msgTitle)) {
            queryWrapper.like("MC_TITLE", msgTitle);
        }
        if (StringUtils.isNotBlank(appCode)) {
            queryWrapper.eq("MC_APP_CODE", appCode);
        }
        if (StringUtils.isNotBlank(msgTo)) {
            queryWrapper.eq("MC_TO", msgTo);
        }


        /**
         * 时间查询条件规则
         * 查询条件开始时间小于数据库开始时间字段，或查询条件结束时间大于数据库结束时间字段
         */
        if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            String dateQuerySql = "((UNIX_TIMESTAMP('" + startDate + " 00:00:00') <= UNIX_TIMESTAMP(MC_SENDTIME) " +
                    "and UNIX_TIMESTAMP('" + endDate + " 23:59:59') >= UNIX_TIMESTAMP(MC_SENDTIME)) " +
                    "or " +
                    "(UNIX_TIMESTAMP('" + startDate + " 00:00:00') <= UNIX_TIMESTAMP(MC_SENDTIME) " +
                    "and UNIX_TIMESTAMP('" + endDate + " 23:59:59') >= UNIX_TIMESTAMP(MC_SENDTIME)))";
            queryWrapper.apply(dateQuerySql);
        }

        queryWrapper.orderByDesc("MC_SENDTIME");
        queryWrapper.in("MC_STATUS", 1, 2);

        IPage<Message> messageList = messageMapper.selectPage(page, queryWrapper);

        return messageList;
    }

    /**
     * 获得未读的消息数量
     *
     * @param condition :
     * @return : java.lang.Integer
     * @author : YunTao.Li
     * @date : 2020/5/7 2020/5/7
     */
    public Integer getUnReadMessageCount(Map<String, Object> condition) throws Throwable {
        String appCode = condition.get("appCode") != null ? condition.get("appCode").toString() : "";
        String empCode = condition.get("empCode") != null ? condition.get("empCode").toString() : "";


        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(appCode)) {
            queryWrapper.eq("MC_APP_CODE", appCode);
        }

        if (StringUtils.isNotBlank(empCode)) {
            queryWrapper.eq("MC_TO", empCode);
        }

        queryWrapper.eq("MC_STATUS", 1);

        Integer count = messageMapper.selectCount(queryWrapper);
        return count;
    }

    public List<Message> getMessageListWithOutPage(Map<String, Object> condition) throws Throwable {
        String mcAppCode = condition.get("appCode") != null ? condition.get("appCode").toString() : "";
        String mcStatus = condition.get("mcStatus") != null ? condition.get("mcStatus").toString() : "";
        String msgTo = condition.get("msgTo") != null ? condition.get("msgTo").toString() : "";


        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(mcAppCode)) {
            queryWrapper.eq("MC_APP_CODE", mcAppCode);
        }

        if (StringUtils.isNotBlank(mcStatus)) {
            queryWrapper.eq("MC_STATUS", mcStatus);
        }

        if (StringUtils.isNotBlank(msgTo)) {
            queryWrapper.eq("MC_TO", msgTo);
        }

        queryWrapper.orderByDesc("MC_SENDTIME");
        List<Message> messageList = messageMapper.selectList(queryWrapper);

        return messageList;
    }

    public Message getMessageByCondition(Map condition) throws Throwable {
        String appCode = condition.get("appCode") != null ? condition.get("appCode").toString() : "";


        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(appCode)) {
            queryWrapper.eq("MC_APP_CODE", appCode);
        }

        queryWrapper.orderByDesc("MC_SENDTIME");
        queryWrapper.last("limit 1");
        Message message = messageMapper.selectOne(queryWrapper);

        return message;
    }

    /**
     * 将message对象插入数据库
     *
     * @param record :
     * @return : void
     * @author : YunTao.Li
     * @date : 2020/3/16 2020/3/16
     */
    public void insertMessage(Message record) throws Throwable {
        messageMapper.insert(record);
    }

    /**
     * 修改message
     *
     * @param record :
     * @return : void
     * @author : YunTao.Li
     * @date : 2020/3/19 2020/3/19
     */
    public void updateMessage(Message record) {
        messageMapper.updateById(record);
    }


    /**
     * 以当前时间为准，精确到秒，查询已到期的消息列表
     *
     * @return : java.util.List<com.primeton.wh.mcenter.impl.message.entity.Message>
     * @author : YunTao.Li
     * @date : 2020/6/5 2020/6/5
     */
    public List<Message> getExpiredMessages() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("MC_STATUS", MessageConstants.MESSAGE_STATUS_EFFECTIVE);

        String currentDate = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
        String dateQuerySql = "UNIX_TIMESTAMP('" + currentDate + "') >= UNIX_TIMESTAMP(MC_EFFECTIVETIME)";
        queryWrapper.apply(dateQuerySql);

        List<Message> messageList = messageMapper.selectList(queryWrapper);
        return messageList;
    }

    public IPage<Message> selectMessage(Map<String, Object> condition) {
        Long currentPage = (Long) condition.get("page");
        Long currentPageSize = (Long) condition.get("limit");

        String msgTitle = condition.get("msgTitle") != null ? condition.get("msgTitle").toString() : "";
        String startDate = condition.get("startDate") != null ? condition.get("startDate").toString() : "";
        String endDate = condition.get("endDate") != null ? condition.get("endDate").toString() : "";
        String appCode = condition.get("appCode") != null ? condition.get("appCode").toString() : "";
        String msgTo = condition.get("msgTo") != null ? condition.get("msgTo").toString() : "";
        String appNameQuery = condition.get("appName") != null ? condition.get("appName").toString() : "";
        String mcStatus = condition.get("mcStatus") != null ? condition.get("mcStatus").toString() : "";

        startDate.concat("00:00:00");
        endDate.concat("23:59:59");

        IPage<Message> page = new Page(currentPage, currentPageSize);

        QueryWrapper queryWrapper = new QueryWrapper();
        /**
         * 时间查询条件规则
         * 查询条件开始时间小于数据库开始时间字段，或查询条件结束时间大于数据库结束时间字段
         */
        if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            String dateQuerySql = "((UNIX_TIMESTAMP('" + startDate + " 00:00:00') <= UNIX_TIMESTAMP(MC_SENDTIME) " +
                    "and UNIX_TIMESTAMP('" + endDate + " 23:59:59') >= UNIX_TIMESTAMP(MC_SENDTIME)) " +
                    "or " +
                    "(UNIX_TIMESTAMP('" + startDate + " 00:00:00') <= UNIX_TIMESTAMP(MC_SENDTIME) " +
                    "and UNIX_TIMESTAMP('" + endDate + " 23:59:59') >= UNIX_TIMESTAMP(MC_SENDTIME)))";
            queryWrapper.apply(dateQuerySql);
        }



        IPage<Message> messageList = messageMapper.selectPage(page,queryWrapper);

        List<Message> selectMessage = messageMapper.selectMessage(page,msgTitle,startDate,endDate,msgTo,appCode,appNameQuery,mcStatus);

        messageList.setRecords(selectMessage);

        return messageList;
    }

}
